package com.example.carmessage2;

import android.app.ActivityOptions;
import android.content.Context;
import android.content.Intent;
import android.hardware.display.DisplayManager;
import android.os.Build;
import android.os.Bundle;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import com.example.carmessage2.message.BasecarActivity;
import com.example.carmessage2.message.CarBoDyActivity;
import com.example.carmessage2.message.CarLunActivity;
import com.example.carmessage2.message.DianChiActivity;
import com.example.carmessage2.message.DianjiDong;
import com.example.carmessage2.message.LowPanActivity;

public class Carmessage extends AppCompatActivity {

    private GridView messagegv;



    private Integer[] img = {R.drawable.base, R.drawable.diandongche, R.drawable.dianchi, R.drawable.carbody, R.drawable.chepan, R.drawable.carlun, R.drawable.taiyaceshi};

    private String[] title = {"基本信息", "电动机信息", "电池信息", "车身信息", "底盘/转向信息", "车轮/制动信息", "胎压监测"};




    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_carmessage);


        messagegv = findViewById(R.id.messagegv);
        messagegv.setAdapter(new GvAdapter());


        Fuping();


        getYiBiaoPan();


        Intent intent = getIntent();
        int shutdown=intent.getIntExtra("shutdown",0);

        if (shutdown==1){

           System.exit(0);


        Intent intent1=new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent1);


        }
        messagegv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long l) {

                switch (position) {
                    case 0:

                        Intent intent = new Intent(Carmessage.this, BasecarActivity.class);

                        startActivity(intent);


                        break;
                    case 1:
                        Intent intent1 = new Intent(Carmessage.this, DianjiDong.class);

                        startActivity(intent1);


                        break;
                    case 2:

                        Intent intent2 = new Intent(Carmessage.this, DianChiActivity.class);

                        startActivity(intent2);


                        break;
                    case 3:
                        Intent intent3 = new Intent(Carmessage.this, CarBoDyActivity.class);

                        startActivity(intent3);


                        break;
                    case 4:
                        Intent intent4 = new Intent(Carmessage.this, LowPanActivity.class);

                        startActivity(intent4);


                        break;
                    case 5:

                        Intent intent5 = new Intent(Carmessage.this, CarLunActivity.class);

                        startActivity(intent5);


                        break;

                    case 6:

                        Fuping();
                        getYiBiaoPan();
                        break;

                }


            }
        });
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    private void getYiBiaoPan() {
        DisplayManager displayManager = (DisplayManager) Carmessage.this.getSystemService(Context.DISPLAY_SERVICE);
        Display[] displays = displayManager.getDisplays();

        if (displays.length > 1) {
            Intent intent = new Intent(Carmessage.this, YiBiaoPan.class);

            intent.setClassName(getPackageName(), getPackageName() + ".YiBiaoPan");

            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);


            ActivityOptions options = null;

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                options = ActivityOptions.makeBasic();
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                options.setLaunchDisplayId(displays[1].getDisplayId());
            }
            startActivity(intent, options.toBundle());
        }

    }


    private class GvAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return img.length;
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View view, ViewGroup viewGroup) {
            view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.gv_item, null);

            ImageView image = view.findViewById(R.id.image);

            TextView text = view.findViewById(R.id.text);

            image.setImageResource(img[position]);
            text.setText(title[position]);
            return view;
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    private void Fuping() {
        DisplayManager displayManager = (DisplayManager) Carmessage.this.getSystemService(Context.DISPLAY_SERVICE);
        Display[] displays = displayManager.getDisplays();

        if (displays.length > 1) {
            Intent intent = new Intent(Carmessage.this, CarMessageFu.class);

            intent.setClassName(getPackageName(), getPackageName() + ".CarMessageFu");

            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);

            ActivityOptions options = null;

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                options = ActivityOptions.makeBasic();
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                options.setLaunchDisplayId(displays[2].getDisplayId());
            }
            startActivity(intent, options.toBundle());
        }
    }
}